//
//  ChageUserInfoVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/3.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "ChageUserInfoVC.h"
#import "InputMessageCell.h"
#import <IQKeyboardManager.h>

@interface ChageUserInfoVC () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *commitBtn;

@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) Country *country;

@property (nonatomic, copy) NSString *phoneNum;
@property (nonatomic, copy) NSString *psw;
@property (nonatomic, copy) NSString *confirmPsw;
@property (nonatomic, copy) NSString *code;

@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic, weak) UIButton *getCodeBtn;

@end

@implementation ChageUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 80;
    [self.commitBtn setTitle:MYLocalizedString(@"PUBLIC_CONFIRM", nil) forState:UIControlStateNormal];
    [self setupData];
}

- (void)setupData {
    
    //    0绑定手机号；1更换手机号；2修改密码
    switch (self.type) {
        case 0: {
            
            self.title = MYLocalizedString(@"MYINFO_LINK_NUMBER", nil);
            self.dataSource = @[@{@"name":MYLocalizedString(@"LOGIN_MOBILE", nil),@"choose":MYLocalizedString(@"REGISTER_PLACE", nil),@"inputPH":MYLocalizedString(@"REGISTER_MOBILE_PLACEHOLDER", nil),@"index":@"0",@"type":@"0"},
                                @{@"name":MYLocalizedString(@"REGISTER_PSW", nil),@"inputPH":MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil),@"index":@"1",@"type":@"1"},
                                @{@"name":MYLocalizedString(@"REGISTER_CODE", nil),@"getCode":MYLocalizedString(@"REGISTER_GET_CODE", nil),@"inputPH":MYLocalizedString(@"REGISTER_CODE_PLACEHOLDER", nil),@"index":@"2",@"type":@"2"}];
        }
            break;
        case 1: {
            
            self.title = MYLocalizedString(@"MYINFO_CHANGE_NUMBER", nil);
            
            self.dataSource = @[@{@"name":MYLocalizedString(@"MYINFO_NUMBER_LINKED", nil),@"namevalue":[NSString stringWithFormat:@"%@ %@",[UserManager sharedManager].user.countrycode,[UserManager sharedManager].user.username],@"index":@"1",@"type":@"4"},
                                @{@"name":MYLocalizedString(@"LOGIN_MOBILE", nil),@"choose":MYLocalizedString(@"REGISTER_PLACE", nil),@"inputPH":MYLocalizedString(@"REGISTER_MOBILE_PLACEHOLDER", nil),@"index":@"0",@"type":@"0"},
                                @{@"name":MYLocalizedString(@"REGISTER_CODE", nil),@"getCode":MYLocalizedString(@"REGISTER_GET_CODE", nil),@"inputPH":MYLocalizedString(@"REGISTER_CODE_PLACEHOLDER", nil),@"index":@"2",@"type":@"2"}];
        }
            break;
        case 2: {
            
            self.title = MYLocalizedString(@"MYINFO_CHANGE_PSW", nil);
            self.dataSource = @[@{@"name":MYLocalizedString(@"REGISTER_PSW", nil),@"inputPH":MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil),@"index":@"1",@"type":@"1"},
                                @{@"name":MYLocalizedString(@"REGISTER_CONFIRM_PSW", nil),@"inputPH":MYLocalizedString(@"REGISTER_INPUTAGAIN_PSW", nil),@"index":@"1",@"type":@"3"},
                                @{@"name":MYLocalizedString(@"REGISTER_CODE", nil),@"getCode":MYLocalizedString(@"REGISTER_GET_CODE", nil),@"inputPH":MYLocalizedString(@"REGISTER_CODE_PLACEHOLDER", nil),@"index":@"2",@"type":@"2"}];
        }
            break;
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = self.dataSource[indexPath.row];
    
    InputMessageCell *cell = [InputMessageCell getCellWithTableView:tableView index:[dict[@"index"] integerValue]];
    
    [cell configWithDict:dict];
    cell.superVC = self;
    cell.delegate = self;
    cell.type = [dict[@"type"] integerValue];
    
    return cell;
    
}

//0手机号/区号；1密码；2验证码；3确认密码

- (void)selectedCountry:(Country *)country type:(NSInteger)type {
    
    if (type == 0) {
        self.country = country;
    }
}
- (void)inputMsg:(NSString *)msg type:(NSInteger)type {
    
    if (type == 0) {
        self.phoneNum = msg;
    } else if (type == 1) {
        self.psw = msg;
    } else if (type == 2) {
        self.code = msg;
    } else if (type == 3) {
        self.confirmPsw = msg;
    }
}

- (void)getCodeAction:(UIButton *)sender {
    
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    
    NSInteger tag = sender.tag;
    if (tag > 0) return;
    self.getCodeBtn = sender;
    
    if (self.type != 2) {
        
        if (!self.country) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_NOCOUNTRY", nil)];
            return;
        }
        
        if ([IGUtil isStringLenght0:self.phoneNum]) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_NOMOBILE", nil)];
            return;
        }
        
        if (self.type == 0) {
            if ([IGUtil isStringLenght0:self.psw]) {
                [IGUtil showToast:MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil)];
                return;
            }
        }
    } else {
        
        if ([IGUtil isStringLenght0:self.psw]) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil)];
            return;
        }
        if (![self.confirmPsw isEqualToString:self.psw]) {
            [IGUtil showToast:@"两次输入不一致"];
            return;
        }
    }
    
    sender.tag = 60; // 作为倒计时的标记
    if (self.countDownTimer != nil) {
        
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
    self.countDownTimer = [NSTimer timerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(updateSendText)
                                                userInfo:nil
                                                 repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.countDownTimer forMode:NSDefaultRunLoopMode];
    [self updateSendText];
    
    //    0注册，认证，1找回密码,2改密码
    [NetAPIManager getCodeWithCountryCode:self.country.code cellPhone:self.phoneNum usage:self.type == 2 ? 2 : 0 Block:^(NSString *result, NSError *error) {
        
        if (error) {
            sender.tag = 0;
        }
        
        if (result) {
            [IGUtil showToast:result];
        }
    }];
}

- (void)updateSendText {
    
    NSInteger tag = self.getCodeBtn.tag;
    NSString* title = tag > 0 ? [NSString stringWithFormat:@"%@(%ld)",MYLocalizedString(@"REGISTER_REGET_CODE", nil),(long)tag] : MYLocalizedString(@"REGISTER_REGET_CODE", nil);
    UIColor* color = tag > 0 ? kContentColor : kThemeColor;
    [self.getCodeBtn setTitle:title forState:UIControlStateNormal];
    [self.getCodeBtn setTitleColor:color forState:UIControlStateNormal];
    
    
    if (tag == 0) {
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
    
    [self.getCodeBtn setTag:tag -1];
}

- (IBAction)confirmAction:(id)sender {
    
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    if (self.type != 2) {
        
        if (!self.country) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_NOCOUNTRY", nil)];
            return;
        }
        
        if ([IGUtil isStringLenght0:self.phoneNum]) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_NOMOBILE", nil)];
            return;
        }
        
        if (self.type == 0) {
            if ([IGUtil isStringLenght0:self.psw]) {
                [IGUtil showToast:MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil)];
                return;
            }
        }
    } else {
        
        if ([IGUtil isStringLenght0:self.psw]) {
            [IGUtil showToast:MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil)];
            return;
        }
        if (![self.confirmPsw isEqualToString:self.psw]) {
            [IGUtil showToast:@"两次输入不一致"];
            return;
        }
    }
    
    if ([IGUtil isStringLenght0:self.code]) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_CODE_PLACEHOLDER", nil)];
        
        return;
    }
    
    if (self.type == 2) {
        
        [NetAPIManager updatepsw:self.psw code:self.code block:^(NSString *result, NSError *error) {
            
            if (!error) {
                [IGUtil showToast:@"密码修改成功，下次请用新密码登录"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
        
        return;
    }
    
    NSString * str = @"";
    if(self.psw.length > 0){
        str = self.psw;
    }
    [NetAPIManager updateCellphoneWithCountryCode:self.country.code cellPhone:self.phoneNum encode:self.code PassWord:str block:^(NSString *result, NSError *error){
        
        if (!error) {
            [IGUtil showToast:MYLocalizedString(@"MYINFO_NUMBER_LINKED", nil)];
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshUserNotif object:nil];
        }
    }];
}


@end

